// SPDX-License-Identifier: GPL-2.0
/*
 * PMEM: DAX
 *
 * (C) 2024.06.29 BuddyZhang1 <buddy.zhang@aliyun.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>

#define MAP_SIZE	(16 * 1024 * 1024) /* 16MB */
#define errExit(msg)	do { perror(msg); exit(EXIT_FAILURE); } while (0)

int main()
{
	void *mem;
	int fd;

	/* OPEN DEVDAX */
	fd = open("/dev/dax0.0", O_RDWR);
	if (fd < 0)
		errExit("OPEN DAX0.0 FAILED\n");	

	/* MAP DAX */
	mem = mmap(NULL, MAP_SIZE,
		   PROT_READ | PROT_WRITE,
		   MAP_SHARED,
		   fd,
		   0);
	if (mem == MAP_FAILED)
		errExit("MMAP DAX0.0 FAILED\n");

	/* ACCESS PMEM, Trigger #PF */
	*(char *)mem = 'B';
	printf("PMEM: %#lx => %c\n", (unsigned long)mem, *(char *)mem);

	/* RECLAIM */
	munmap(mem, MAP_SIZE);
	close(fd);
	
	return 0;
}
